//
//  VisitorLoginView.swift
//  微博项目
//
//  Created by mac on 16/1/11.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

import SnapKit


//声明协议
@objc protocol VisitorLoginViewDelegate: NSObjectProtocol{

    //协议方法
    //用户将要注册
    optional func userWillRegister()
    
    //用户将要登录
    func userWillLogin()
    

}
class VisitorLoginView: UIView {
    
    weak var visitorViewDelegate : VisitorLoginViewDelegate?
    
    //MARK :按钮的监听事件方法
 @objc private func loginBtnDidClick(){
    
        //使用代理调用协议方法
        visitorViewDelegate?.userWillLogin()
    
    }
    
  @objc private func registerBtnDidClick(){
        
        //使用代理调用协议方法
        visitorViewDelegate?.userWillRegister?()
        
    }
    
    //外界调用的方法
    func setUIWithInfo(imageName:String?,tipText:String?){

        if let img = imageName {
        
        cicleIcon.image = UIImage(named: img)
            
            //隐藏大房子
            largeHouse.hidden = true
        bringSubviewToFront(cicleIcon)
        }
        else{
        //说明在主页
            startAnimation()
        
        }
        tipLabel.text = tipText
    }
    
    //圆圈的动画效果
    func startAnimation(){
    //基础动画
        let base = CABasicAnimation(keyPath: "transform.rotation")
        
        //设置重复次数
        base.repeatCount = MAXFLOAT
        base.toValue = 2 * M_PI
        base.duration = 10
        
        cicleIcon.layer.addAnimation(base, forKey: nil)
    }

    lazy var largeHouse:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    lazy var cicleIcon : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    lazy var backView :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    lazy var loginButton:UIButton = {
    let btn = UIButton()
        
        //设置按钮的文字
        btn.setTitle("登录", forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(16)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
   
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        
        return btn
    
    }()
    
    //注册按钮
  lazy var registerBtn : UIButton = {
        let btn = UIButton()
    
        btn.setTitle("注册", forState: .Normal)
        btn.titleLabel?.text = "hahahah"
    btn.titleLabel?.font = UIFont.systemFontOfSize(16)
    btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
    
    btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
    return btn
    
    }()
    
    //提示文案
    lazy var tipLabel : UILabel = {
    let l = UILabel()
        
    l.font = UIFont.systemFontOfSize(14)
        l.text = "登录后,别人评论你的微薄别人评论你的微薄别人评论你的微"
        l.numberOfLines = 0
        l.textColor = UIColor.darkGrayColor()
        l.sizeToFit()
        return l
    }()
    //加载子视图
    func setUI(){
        addSubview(largeHouse)
        addSubview(cicleIcon)
        addSubview(backView)
        addSubview(loginButton)
        addSubview(registerBtn)
        addSubview(tipLabel)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
        
        //环形图片的位置
        cicleIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(snp_centerX)
            make.centerY.equalTo(snp_centerY).offset(-80)
        }
        
        //房子的位置
        largeHouse.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(cicleIcon.snp_centerX)
            make.centerY.equalTo(cicleIcon.snp_centerY)
        }
        
        //背景图片
        backView.snp_makeConstraints { (make) -> Void in
//            make.top.equalTo(self.snp_top)
//            make.bottom.equalTo(self.snp_bottom).offset(-200)
//            make.left.equalTo(self.snp_left)
//            make.right.equalTo(self.snp_right)
            
            //链式约束
            make.top.left.right.equalTo(self)
            make.bottom.equalTo(registerBtn.snp_bottom).offset(-30)
        }
        
        //登录按钮的位置
        loginButton.snp_makeConstraints { (make) -> Void in
//            make.leading.equalTo(cicleIcon.snp_leading)
            make.centerX.equalTo(tipLabel.snp_centerX).offset(-80)
            make.top.equalTo(tipLabel.snp_bottom).offset(20)
            make.width.equalTo(80)
            
        }
        //注册按钮的位置
        registerBtn.snp_makeConstraints { (make) -> Void in
//            make.centerX.equalTo(loginButton.snp_centerX).offset(100)
            make.top.equalTo(tipLabel.snp_bottom).offset(20)
            make.trailing.equalTo(tipLabel.snp_trailing)
            make.width.equalTo(80)
        }
        
        //提示文案
        tipLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(cicleIcon.snp_bottom)
            make.leading.equalTo(cicleIcon.snp_leading)
            make.width.equalTo(200)
        }
        
        self.backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        loginButton.addTarget(self, action: "loginBtnDidClick", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "registerBtnDidClick", forControlEvents: .TouchUpInside)
        
    }

    
   
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

}
